/*/

	Written By:					Leong C. W., Johnathon

	Written Date:				17 / July / 2008
	
	Email:						johnathon dot leong at gmail dot com
	
	Operating System Used:		Ubuntu 8.04
	
	IDE:						"gedit" from Ubuntu 8.04 is used
	
	Programming Language:		C++
	
	Compiler:					g++
	
	Disclaimer:					Copyright (c) 2008	Leong C. W., Johnathon
								
								Permission is hereby granted, free of charge, to any person obtaining a copy
								of this software and associated documentation files (the "Software"), to
								deal in the Software without restriction, including without limitation the
								rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
								sell copies of the Software, and to permit persons to whom the Software is
								furnished to do so, subject to the following conditions:

								The above copyright notice and this permission notice shall be included in
								all copies or substantial portions of the Software.

								THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
								IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
								FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
								AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
								LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
								FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
								IN THE SOFTWARE.


------------------------------------------------------------------------------------------------------------

	Function Name:				int linux_kbhit(void);
	
	Function Purpose:			This function is to provide similar functionality as the kbhit() function
								found in Visual C++. This function is non-blocking at kbhit(), therefore
								useful to user who wants to detect key values as subfunction selection such
								as <Esc> key to quit program or <1> or <2> key as user input selection.
								However, this fucntion will not be able to detect some special keys such as
								<Caps Lock>, <Shift> and <Ctrl> or some combination key such as <Ctrl> + <c>
								or <Ctrl> + <z>.

	Number of Input Argument:	N/A
	
	Input Argument type:		N/A
	
	Purpose of Input Argument:	N/A
		
	Return Value type:			int
	
	Purpose of Return Value:	The returned integer value contains the integer interpretation of the key
								pressed on the keyboard. Some keys on the keyboard may return 1 or more sets
								of integer value.
								
								etc: <Insert> key may return the following values sets:	27
																						91
																						50
																						126

	Known Issues:				- No known issue found when running program using Ubuntu 8.04 Terminal.
								- No known issue found when running program using Fedora 9 Terminal.

								- Program will experience some different behaviour from Ubuntu Terminal when
								  used with Eclipse SDK and displaying off Eclipse SDK Console window.

									- Keyboard value will not be return unless <Enter> key is hit.

									- Special keys such as <Insert>, <Home>, <Delete> will not have return
									  values when hit.

/*/

#ifndef LINUX_KBHIT_H_
#define LINUX_KBHIT_H_

#include <stdio.h>
#include <termios.h>
#include <unistd.h>

int linux_kbhit(void)
{
	struct termios oldt, newt;
	int ch;

	tcgetattr( STDIN_FILENO, &oldt );
	newt = oldt;

	newt.c_cc[VMIN] = 0;
	newt.c_cc[VTIME] = 1;
	newt.c_lflag &= ~( ICANON | ECHO );
	tcsetattr( STDIN_FILENO, TCSANOW, &newt );

	ch = getchar();

	tcsetattr( STDIN_FILENO, TCSANOW, &oldt );

	return ch;
}

#endif

